/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connection;

import authentication.EncryptionType;
import data.AccountData;
import data.Data;
import data.email.Attachment;
import data.email.EmailFolder;
import data.email.EmailMessage;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Session;
import javax.mail.Store;
import com.sun.mail.util.MailSSLSocketFactory;

/**
 * Třída reprezentující IMAP účet. Umožňuje příjem zpráv.
 *
 * @author Arcanis
 */
public class IMAPAccount extends Account implements IReceiver {

    protected Store mailStore = null;
    private List<EmailFolder> folders = null;

    IMAPAccount(AccountData data) throws Exception {
        super(data);

        createStore();
    }

    private boolean createStore() throws Exception {
        Properties props = new Properties();
        String proto = null;
        if (this.encryption.equals(EncryptionType.none)) {
            proto = "imap";
        } else {
            proto = "imaps";
            props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.setProperty("mail.imaps.socketFactory.fallback", "false");
            props.setProperty("mail.imaps.ssl.trust", "*");
            props.setProperty("mail.imaps.connectionpoolsize", "100");
            
            /*
             * props.setProperty("mail.imaps.port", this.);
             * props.setProperty("mail.imaps.socketFactory.port", );
             */
        }


        props.put("mail.store.protocol", proto);
        props.setProperty("mail.imaps.partialfetch", "false");
        Session session = Session.getDefaultInstance(props);
        try {
            this.mailStore = session.getStore(proto);
        } catch (Exception e) {
            throw e;
        }
        return true;
    }

    public boolean connectToStore() {
        if (this.mailStore != null && !this.mailStore.isConnected()) {
            try {
                this.mailStore.connect(this.serverName, this.port, this.authenticationData.getLogin(), this.authenticationData.getPassword());
                return true;
            } catch (Exception e) {
                //@TODO what
            }
        }
        return false;
    }

    public void disconnectFromStore() {
        if (this.mailStore != null && this.mailStore.isConnected()) {
            try {
                this.mailStore.close();
            } catch (Exception e) {
                // @TODO wtf
            }
        }
    }

    @Override
    public List<EmailFolder> loadAllFolders() throws Exception {
        this.folders = new LinkedList<>();

        this.connectToStore();

        Folder defaultFolder = mailStore.getDefaultFolder();
        if (defaultFolder != null) {
            EmailFolder newFolder = null;
            for (Folder currentFolder : defaultFolder.list()) {
                newFolder = new EmailFolder(currentFolder, this);
                folders.add(newFolder);
            }
        }
        this.disconnectFromStore();

        return this.folders;
    }

    @Override
    public void updateMessages(Data data) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<EmailFolder> getAllCachedFolders() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public EmailFolder getEmailFolder(String folderName) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public EmailFolder getEmailFolder(int number) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deleteFolder(String folderName) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deleteFolder(int number) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
